﻿using Microsoft.Xna.Framework.Graphics;

namespace Babylon.Toolbox
{
    public sealed class ModelMeshPart
    {
        readonly ModelMesh modelMesh;

        public ModelEffect Effect { get; set; }

        public int MinVertexIndex { get; set; }

        public int NumVertices { get; set; }

        public int StartIndex { get; set; }

        public int PrimitivesCount { get; set; }

        public int VerticesOffset { get; set; }
        public int IndicesOffset { get; set; }

        public ModelMeshPart(ModelMesh modelMesh, bool copyMeshOffsets = true) 
        {
            this.modelMesh = modelMesh;
            this.modelMesh.AddPart(this);
            if (copyMeshOffsets){
                VerticesOffset = modelMesh.VerticesOffset;
                IndicesOffset = modelMesh.IndicesOffset;
            }

        }

        internal void Draw()
        {
            if (Effect == null)
                return;

            Effect.Apply();
            modelMesh.Device.DrawIndexedPrimitives(PrimitiveType.TriangleList, VerticesOffset, MinVertexIndex, NumVertices, IndicesOffset + StartIndex, PrimitivesCount);
        }

        public override string ToString()
        {
            return string.Format("NumVertices:{0} StartIndex:{1} PrimitivesCount:{2}", NumVertices, StartIndex, PrimitivesCount);
        }
    }
}
